/*******************************************************************************
 ** Name: fuzzy_mod.cpp                                                       **
 ** Description:                         **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the LICENSE file within             **
 ** this distribution or at $WIKI/display/GOVT/License-software.              **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $GAME_DIR/LICENSE.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 ** Copyright (c) 2009-2012  Barry Gian James <jamesbl@math.colorado.edu>     **
 ** All rights reserved.                                                      **
 **                                                                           **
 ** Ref: $HeadURL$
 ******************************************************************************/

#include "fuzzy_mod.h"

namespace Falcon {
namespace Ext {

//////////////////////////////////////[class FuzzyCategory
FuzzyCategory::FuzzyCategory()
		: ClassUser("FuzzyCategory"),
		  FALCON_INIT_METHOD(Range),
		  FALCON_INIT_METHOD(Membership),
		  FALCON_INIT_PROPERTY(low),
		  FALCON_INIT_PROPERTY(mid),
		  FALCON_INIT_PROPERTY(high)
{

}

FuzzyCategory::~FuzzyCategory() { }

//======================[ Methods
FALCON_DEFINE_METHOD_P(FuzzyCategory,Range)
{
	Item * iLow = ctx->param(0);
	Item * iMid = ctx->param(1);
	Item * iHigh = ctx->param(2);

	if (iLow == 0 || iMid == 0 || iHigh == 0 ||
		!iLow->isNumeric() || !iMid->isNumeric() || !iHigh->isNumeric())
	{
		throw paramError(__LINE__, SRC);
	}

//	Property_high.set(this,iHigh->asNumeric());
//	Property_mid.iMid = iMid->asNumeric();
//	Property_low.iLow = iLow->asNumeric();

	low = *iLow->asNumeric();
	mid = *iMid->asNumeric();
	high = *iHigh->asNumeric();

	ctx->returnFrame();
}

FALCON_DEFINE_METHOD_P(FuzzyCategory, Membership)
{
//	Item output, midlow, highmid;
	double output, midlow, highmid;
	Falcon::Item imid, ilow, ihigh;

	Item * input = ctx->param(0);

	midlow = mid - low;
	highmid = high - mid;

	if ( (*input <= low) || (*input >= high))
		output = 0;
	else
	{
		if (*input > mid)
			output = (high - *input)/highmid;
		else if (*input == mid)
			output = 1.0;
		else
			output = (*input - low)/midlow;
	}


	ctx->returnFrame(output);
}

//======================[ Properties
FALCON_DEFINE_PROPERTY_SET_P(FuzzyCategory,low)
{
	checkType(value.isNumeric(), "N");
	low = value.asNumeric();
}

FALCON_DEFINE_PROPERTY_GET_P(FuzzyCategory,low)
{
	value.setNumeric(low);
}

FALCON_DEFINE_PROPERTY_SET_P(FuzzyCategory,mid)
{
	checkType(value.isNumeric(), "N");
	mid = *value.asNumeric();
}

FALCON_DEFINE_PROPERTY_GET_P(FuzzyCategory,mid)
{
	value.setNumeric(mid);
}

FALCON_DEFINE_PROPERTY_SET_P(FuzzyCategory,high)
{
	checkType(value.isNumeric(), "N");
	high = *value.asNumeric();
}

FALCON_DEFINE_PROPERTY_GET_P(FuzzyCategory,high)
{
	value.setNumeric(high);
}


}	/* namespace Ext */
}	/* namespace Falcon */


/* end of fuzzy_mod.cpp */

